home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / 3Dmodeling / tracktest.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  6.9 KB  |  371 lines

  1. /*
  2.  * Copyright 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17.  
  18. /* Tom Davis -- 1992 */
  19.  
  20. /* draw worm */
  21.  
  22. #include <stdio.h>
  23. #include <math.h>
  24. #include <gl.h>
  25. #include <device.h>
  26. #include "3d.h"
  27.  
  28. #define PI  3.1415926535
  29. #define EPSILON        .0001
  30.  
  31. #define SSTEPS    400
  32. #define NORMFACTOR 80.0
  33.  
  34. void drawsurface();
  35.  
  36. float  blackvec[3] = {0.0, 0.0, 0.0};
  37. static float idmat[4][4] = {1.0,0.0,0.0,0.0,
  38.                 0.0,1.0,0.0,0.0,
  39.         0.0,0.0,1.0,0.0,
  40.         0.0,0.0,0.0,1.0};
  41.  
  42. float  shiny_material[] = {SPECULAR, 0.8, 0.8, 0.8,
  43.                  DIFFUSE,  0.4, 0.4, 0.4,
  44.                      SHININESS, 30.0,
  45.                  LMNULL};
  46.  
  47. float  purple_material[] = {SPECULAR, 0.3, 0.3, 0.3,
  48.                 DIFFUSE,  0.8, 0.0, 0.8,
  49.                 SHININESS,5.0,
  50.                 AMBIENT,  0.2, 0.0, 0.2, 
  51.                 LMNULL};
  52.  
  53. float  material1[] = {SPECULAR, 0.3, 0.3, 0.3,
  54.                 DIFFUSE,  0.8, 0.0, 0.8,
  55.                 SHININESS,5.0,
  56.                 LMNULL};
  57.  
  58. float  material2[] = {SPECULAR, 0.3, 0.3, 0.8,
  59.                 DIFFUSE,  0.0, 0.0, 0.8,
  60.                 SHININESS,2.0,
  61.                 AMBIENT,  0.2, 0.0, 0.2, 
  62.                 LMNULL};
  63.  
  64. float  material3[] = {SPECULAR, 0.3, 0.3, 0.3,
  65.                 DIFFUSE,  0.8, 0.0, 0.0,
  66.                 SHININESS,50.0,
  67.                 AMBIENT,  0.2, 0.0, 0.0, 
  68.                 LMNULL};
  69.  
  70. float  material4[] = {SPECULAR, 0.3, 0.3, 0.3,
  71.                 DIFFUSE,  0.0, 0.8, 0.8,
  72.                 SHININESS,5.0,
  73.                 LMNULL};
  74.  
  75. float  blue_light[] = {LCOLOR, 0.0, 0.0, 0.6,
  76.                POSITION, 0.0, 1.0, 0.0, 0.0,
  77.                LMNULL};
  78.  
  79. void drawworld()
  80. {
  81.     c3f(blackvec);
  82.     clear();
  83.     zclear();
  84.     drawsurface();
  85. }
  86.  
  87. /*
  88. ** Tell the Graphics Library to DEFINE a simple lighting model
  89. ** that accounts for diffuse and ambient reflection.  This simple
  90. ** lighting model happens to be the default lighting model for 
  91. ** the Graphics Library.
  92. */
  93. def_simple_light_model()
  94.  
  95. {
  96.     lmdef(DEFLMODEL, 1, 0, NULL);
  97.     lmdef(DEFMATERIAL, 1, 11, shiny_material);
  98.     lmdef(DEFMATERIAL, 2, 15, purple_material);
  99.     lmdef(DEFMATERIAL, 3, 11, material1);
  100.     lmdef(DEFMATERIAL, 4, 15, material2);
  101.     lmdef(DEFMATERIAL, 5, 15, material3);
  102.     lmdef(DEFMATERIAL, 6, 11, material4);
  103.     lmdef(DEFLIGHT, 1, 0, NULL);
  104.     lmdef(DEFLIGHT, 2, 10, blue_light);
  105. }
  106.  
  107. /*
  108. ** Tell the Graphics Library to USE the simple lighting model
  109. ** that we defined earlier.
  110. */
  111. use_simple_light_model()
  112.  
  113. {
  114.     lmbind(LMODEL, 1);
  115.     lmbind(LIGHT0, 1);
  116.     lmbind(LIGHT1, 2);
  117. }
  118.  
  119. float vectlist[200000][3];
  120. float normlist[200000][3];
  121. long vcount = 0;
  122.  
  123. float trilist[100000][3];
  124. float trinorm[100000][3];
  125. long tcount;
  126.  
  127. void savefunc(type, n0, v0, n1, v1, n2, v2, n3, v3)
  128. long    type;
  129. float    *n0, *n1, *n2, *n3, *v0, *v1, *v2, *v3;
  130. {
  131.     if (type == ADD_QUAD) {
  132.     if (vcount > 200000) {
  133.         fprintf(stderr, "too many points\n");
  134.         exit();
  135.     }
  136.     copy3(n0, &normlist[vcount][0]);
  137.     copy3(v0, &vectlist[vcount++][0]);
  138.     copy3(n1, &normlist[vcount][0]);
  139.     copy3(v1, &vectlist[vcount++][0]);
  140.     copy3(n2, &normlist[vcount][0]);
  141.     copy3(v2, &vectlist[vcount++][0]);
  142.     copy3(n3, &normlist[vcount][0]);
  143.     copy3(v3, &vectlist[vcount++][0]);
  144.     } else {
  145.     if (tcount > 100000) {
  146.         fprintf(stderr, "too many points\n");
  147.         exit();
  148.     }
  149.     copy3(n0, &trinorm[tcount][0]);
  150.     copy3(v0, &trilist[tcount++][0]);
  151.     copy3(n1, &trinorm[tcount][0]);
  152.     copy3(v1, &trilist[tcount++][0]);
  153.     copy3(n2, &trinorm[tcount][0]);
  154.     copy3(v2, &trilist[tcount++][0]);
  155.     }
  156. }
  157.  
  158. void drawsurface()
  159. {
  160.     long i;
  161.  
  162.     for (i = 0; i < vcount;) {
  163.     bgnpolygon();
  164.     n3f(&normlist[i][0]);
  165.     v3f(&vectlist[i++][0]);
  166.     n3f(&normlist[i][0]);
  167.     v3f(&vectlist[i++][0]);
  168.     n3f(&normlist[i][0]);
  169.     v3f(&vectlist[i++][0]);
  170.     n3f(&normlist[i][0]);
  171.     v3f(&vectlist[i++][0]);
  172.     endpolygon();
  173.     }
  174.     for (i = 0; i < tcount;) {
  175.     bgnpolygon();
  176.     n3f(&trinorm[i][0]);
  177.     v3f(&trilist[i++][0]);
  178.     n3f(&trinorm[i][0]);
  179.     v3f(&trilist[i++][0]);
  180.     n3f(&trinorm[i][0]);
  181.     v3f(&trilist[i++][0]);
  182.     endpolygon();
  183.     }
  184. }
  185. #ifdef NOTDEF
  186. #define R 3
  187. #define r .7
  188.  
  189. float x(float s, float t)
  190. {
  191.     return cos(s)*(R+r*cos(t));
  192. }
  193.  
  194. float y(float s, float t)
  195. {
  196.     return sin(s)*(R+r*cos(t));
  197. }
  198.  
  199. float z(float s, float t)
  200. {
  201.     return r*sin(t);
  202. }
  203.  
  204. float xs(float s, float t)
  205. {
  206.     return -sin(s)*(R+r*cos(t));
  207. }
  208.  
  209. float ys(float s, float t)
  210. {
  211.     return cos(s)*(R+r*cos(t));
  212. }
  213.  
  214. float zs(float s, float t)
  215. {
  216.     return 0.0;
  217. }
  218.  
  219. float xt(float s, float t)
  220. {
  221.     return -cos(s)*(r*sin(t));
  222. }
  223.  
  224. float yt(float s, float t)
  225. {
  226.     return -sin(s)*(r*sin(t));
  227. }
  228.  
  229. float zt(float s, float t)
  230. {
  231.     return r*cos(t);
  232. }
  233. #endif /* NOTDEF */
  234.  
  235. float x(float s, float t)
  236. {
  237.     return s;
  238. }
  239.  
  240. float y(float s, float t)
  241. {
  242.     return t;
  243. }
  244.  
  245. float z(float s, float t)
  246. {
  247.     return cos(1.5*(s*s+t*t));
  248. }
  249.  
  250. float xs(float s, float t)
  251. {
  252.     return 1.0;
  253. }
  254.  
  255. float ys(float s, float t)
  256. {
  257.     return 0.0;
  258. }
  259.  
  260. float zs(float s, float t)
  261. {
  262.     return -3.0*s*sin(1.5*(s*s+t*t));
  263. }
  264.  
  265. float xt(float s, float t)
  266. {
  267.     return 0.0;
  268. }
  269.  
  270. float yt(float s, float t)
  271. {
  272.     return 1.0;
  273. }
  274.  
  275. float zt(float s, float t)
  276. {
  277.     return -3.0*t*sin(1.5*(s*s+t*t));
  278. }
  279.  
  280. float al = 3.0;
  281. #ifdef NOTDEF
  282. float x(float s, float t)
  283. {
  284.     return 3*s;
  285. }
  286.  
  287. float y(float s, float t)
  288. {
  289.     return 3*t;
  290. }
  291.  
  292. float z(float s, float t)
  293. {
  294.     float u;
  295.     s = fabs(s); t = fabs(t);
  296.     u = 1.0 - pow(s, al) - pow(t, al);
  297.     if (u <= 0.0) return 0.0;
  298.     return 3*pow(u, 1/al);
  299. }
  300.  
  301. float xs(float s, float t)
  302. {
  303.     return 1.0;
  304. }
  305.  
  306. float ys(float s, float t)
  307. {
  308.     return 0.0;
  309. }
  310.  
  311. float zs(float s, float t)
  312. {
  313.     float u;
  314.     s = fabs(s); t = fabs(t);
  315.     u = 1.0 - pow(s, al) - pow(t, al);
  316.     if (u <= 0.0) return 0.0;
  317.     return -pow(u, 1/al-1)*pow(s, al-1);
  318. }
  319.  
  320. float xt(float s, float t)
  321. {
  322.     return 0.0;
  323. }
  324.  
  325. float yt(float s, float t)
  326. {
  327.     return 1.0;
  328. }
  329.  
  330. float zt(float s, float t)
  331. {
  332.     float u;
  333.     s = fabs(s); t = fabs(t);
  334.     u = 1.0 - pow(s, al) - pow(t, al);
  335.     if (u <= 0.0) return 0.0;
  336.     return -pow(u, 1/al-1)*pow(t, al-1);
  337. }
  338. #endif
  339.  
  340. float zero[3] = {0.0, 0.0, 0.0};
  341. float one[3] = {1.0, 1.0, 1.0};
  342. float minusone[3] = {-1.0, -1.0, -1.0};
  343.  
  344. main(argc, argv)
  345. int argc;
  346. char **argv;
  347. {
  348.     long    i=0, j=0;
  349.     curve_t    *c;
  350.     float    theta, t1, t2, t3;
  351.     if (argc == 2) al = atof(argv[1]);
  352.     /* surface */
  353.     analyticsurface(x, y, z, xs, ys, zs, xt, yt, zt,
  354.             100, -3, 3, 
  355.             100, -3, 3, 0, savefunc);
  356.     keepaspect(1, 1);
  357.     foreground();
  358.     winopen("worm");
  359.     backface(1);
  360.     RGBmode();
  361.     doublebuffer();
  362.     zbuffer(TRUE);
  363.     gconfig();
  364.     mmode(MVIEWING);
  365.     perspective(450, 1.0, 1.0, 1000.0);
  366.     def_simple_light_model();
  367.     use_simple_light_model();
  368.     lmbind(MATERIAL, 6);
  369.     trackmodel(drawworld, 15.0, 0.8, -0.8, 0.8);
  370. }
  371.